import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import javax.sound.midi.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.IOException;

public class it extends World
{
    Sequencer sequencer;
    private String file;

    GreenfootImage map = new GreenfootImage(600,400);
    GreenfootImage sword = new GreenfootImage("Sword.bmp");
    GreenfootImage gun = new GreenfootImage("Gun.bmp");
    GreenfootImage en0 = new GreenfootImage("En0.bmp");
    GreenfootImage bullet = new GreenfootImage("bullet.bmp");
    GreenfootImage en1 = new GreenfootImage("En1.bmp");
    GreenfootImage projectile = new GreenfootImage("projectile.bmp");
    GreenfootImage fall = new GreenfootImage("fall.bmp");
    GreenfootImage end = new GreenfootImage("ZonyEnd.bmp");
    GreenfootImage bat = new GreenfootImage("En2.bmp");
    GreenfootImage lever = new GreenfootImage("button.bmp");
    GreenfootImage elevator = new GreenfootImage("elevator.bmp");
    GreenfootImage dropper = new GreenfootImage("dropper.bmp");
    GreenfootImage glob = new GreenfootImage("glob.bmp");
    GreenfootImage robo = new GreenfootImage("En3.bmp");
    GreenfootImage turret = new GreenfootImage("turret.bmp");
    GreenfootImage plat = new GreenfootImage("platform.bmp");
   
    Color alpha = new Color(0,0,0,0);
    boolean caught=true;
    int weapon=0,closed=0;

    
    public it(){    
        super(600,400, 1);
        file = "sounds/Zelda.mid";
        load(file);
        
        addObject(new control(1),300,200);
        setPaintOrder(control.class , door.class,overlay.class,projectile.class,elevator.class,sword.class,upgrade.class,bat.class,fall.class,Tank.class,Enemy.class,bullet.class,platform.class,Zony.class,lever.class);
        setActOrder(elevator.class,Zony.class);
        addObject(new Zony(0,0),80,360);
        setBackground("Back1.bmp");
        map = new GreenfootImage("Map1.bmp");
        addObject(new overlay(),300,200);
  
        for(int x=0;x<5;x++){
                for(int y=0;y<13;y++){
                    if(sword.getColorAt(x,y).equals(Color.white))
                    sword.setColorAt(x,y,alpha);
                }
            }
        for(int x=0;x<7;x++){
                for(int y=0;y<4;y++){
                    if(gun.getColorAt(x,y).equals(Color.white))
                    gun.setColorAt(x,y,alpha);
                }
            }
        for(int x=0;x<15;x++){
                for(int y=0;y<10;y++){
                    if(en0.getColorAt(x,y).equals(Color.white))
                    en0.setColorAt(x,y,alpha);
                }
            }
        for(int x=0;x<20;x++){
                for(int y=0;y<5;y++){
                    if(bullet.getColorAt(x,y).equals(Color.white))
                    bullet.setColorAt(x,y,alpha);
                }
            }
       for(int x=0;x<60;x++){
                for(int y=0;y<20;y++){
                    if(en1.getColorAt(x,y).equals(Color.white))
                    en1.setColorAt(x,y,alpha);
                }
            }
       for(int x=0;x<48;x++){
                for(int y=0;y<6;y++){
                    if(projectile.getColorAt(x,y).equals(Color.white))
                    projectile.setColorAt(x,y,alpha);
                }
            }
       for(int x=0;x<10;x++){
                for(int y=0;y<10;y++){
                    if(fall.getColorAt(x,y).equals(Color.white))
                    fall.setColorAt(x,y,alpha);
                }
            }
       for(int x=0;x<28;x++){
                for(int y=0;y<28;y++){
                    if(end.getColorAt(x,y).equals(Color.white))
                    end.setColorAt(x,y,alpha);
                }
            }
       for(int x=0;x<45;x++){
                for(int y=0;y<7;y++){
                    if(bat.getColorAt(x,y).equals(Color.white))
                    bat.setColorAt(x,y,alpha);
                }
            }
      for(int x=0;x<6;x++){
                for(int y=0;y<10;y++){
                    if(lever.getColorAt(x,y).equals(Color.white))
                    lever.setColorAt(x,y,alpha);
                }
            }
      for(int x=0;x<35;x++){
                for(int y=0;y<10;y++){
                    if(elevator.getColorAt(x,y).equals(Color.white))
                    elevator.setColorAt(x,y,alpha);
                }
            }
      for(int x=0;x<35;x++){
                for(int y=0;y<11;y++){
                    if(dropper.getColorAt(x,y).equals(Color.white))
                    dropper.setColorAt(x,y,alpha);
                    if(plat.getColorAt(x,y).equals(Color.white))
                    plat.setColorAt(x,y,alpha);
                }
            }
      for(int x=0;x<22;x++){
                for(int y=0;y<12;y++){
                    if(glob.getColorAt(x,y).equals(Color.white))
                    glob.setColorAt(x,y,alpha);
                }
            }
      for(int x=0;x<12;x++){
                for(int y=0;y<28;y++){
                    if(robo.getColorAt(x,y).equals(Color.white))
                    robo.setColorAt(x,y,alpha);
                }
            }
      for(int x=0;x<10;x++){
                for(int y=0;y<18;y++){
                    if(turret.getColorAt(x,y).equals(Color.white))
                    turret.setColorAt(x,y,alpha);
                }
            }
    }
    
    public void act(){
        if (sequencer.getTickPosition() == sequencer.getTickLength())
        {
            sequencer.setTickPosition(0);
            sequencer.start();
        }
    }
    
    public boolean ok(int x,int y){
        return !(map.getColorAt(x,y).equals(Color.cyan) || map.getColorAt(x,y).equals(Color.black) || (map.getColorAt(x,y).equals(Color.blue)&&closed!=0));
    }
    public boolean kill(int x,int y){
        return map.getColorAt(x,y).equals(Color.red);
    }
    
    public boolean isCaught(){
        return caught;
    }
    
    public void caught(){
        caught=true;
    }
        
    public void thrown(){
        caught=false;
    }
    
    public GreenfootImage getSword(){
        return sword;
    }
    public GreenfootImage getGun(){
        return gun;
    }
    public GreenfootImage getEn0(){
        return en0;
    }
    public GreenfootImage getBullet(){
        return bullet;
    }
    public GreenfootImage getEn1(){
        return en1;
    }
    public GreenfootImage getProjectile(){
        return projectile;
    }
    public GreenfootImage getFall(){
        return fall;
    }
    public GreenfootImage getEnd(){
        return end;
    }
    public GreenfootImage getEn2(){
        return bat;
    }
    public GreenfootImage getLever(){
        return lever;
    }
    public GreenfootImage getElevator(){
        return elevator;
    }
    public GreenfootImage getDropper(){
        return dropper;
    }
    public GreenfootImage getGlob(){
        return glob;
    }
    public GreenfootImage getRobo(){
        return robo;
    }
    public GreenfootImage getTurret(){
        return turret;
    }
    public GreenfootImage getPLatform(){
        return plat;
    }
    public GreenfootImage getOverlay(){
        GreenfootImage back = getBackground();
        GreenfootImage overlay = new GreenfootImage(600,400);
        for(int x=0;x<600;x++){
            for(int y=0;y<400;y++){
                if(map.getColorAt(x,y).equals(Color.green) || map.getColorAt(x,y).equals(Color.cyan))
                    overlay.setColorAt(x,y,back.getColorAt(x,y));
                //overlay.setColorAt(x,y,Color.green);
                }
            }
        return overlay;
    }
    
    public void Level(int l){
        if(l==1){
            addObject(new Zony(0,0),80,360);
            setBackground("Back1.bmp");
            map = new GreenfootImage("Map1.bmp");
        }
        if(l==2){
            map = new GreenfootImage("Map2.bmp");
            addObject(new Zony(0,weapon),24,71);
            addObject(new Enemy(true,true),190,190);
            addObject(new Enemy(true,true),300,330);
            addObject(new upgrade(1),44,365);
            addObject(new door(63,0),204,303);
            addObject(new lever(true),97,371);
            setBackground("Back2.bmp");
            closed=1;
        }
        if(l==3){
            map = new GreenfootImage("Map3.bmp");
            addObject(new Zony(1,1),22,70);
            addObject(new lever(true),434,72);
            addObject(new door(64,90),69,86);
            addObject(new Enemy(true,true),539,378);
            addObject(new Enemy(true,true),300,79);
            addObject(new Enemy(true,true),210,265);
            setBackground("Back3.bmp");
            closed=1;
        }
        if(l==4){
            map = new GreenfootImage("Map4.bmp");
            addObject(new Zony(1,1),22,70);
            addObject(new door(103,90),106,86);
            addObject(new door(103,90),233,86);
            addObject(new door(103,-90),360,85);
            addObject(new door(103,-90),487,85);
            addObject(new lever(true),589,72);
            addObject(new Enemy(true,true),448,321);
            addObject(new Enemy(true,true),125,195);
            addObject(new dropper(true),510,255);
            addObject(new bat(),562,177);
            addObject(new bat(),434,170);
            addObject(new bat(),80,191);
            addObject(new bat(),52,96);
            addObject(new bat(),29,245);
            setBackground("Back4.bmp");
            closed=1;
        }
        if(l==5){
            map = new GreenfootImage("Map5.bmp");
            addObject(new Zony(1,1),16,355);
            addObject(new elevator(330),44,374);
            addObject(new elevator(228),574,374);
            addObject(new dropper(true),365,374);
            addObject(new lever(true),494,360);
            addObject(new lever(true),589,80);
            addObject(new dropper(true),365,374);
            addObject(new door(114,90),329,143);
            addObject(new door(34,90),329,91);
            addObject(new door(49,0),555,229);
            addObject(new door(49,0),594,229);
            addObject(new upgrade(2),329,237);
            addObject(new Enemy(true,true),469,136);
            addObject(new Enemy(true,true),205,136);
            addObject(new bat(),525,10);
            addObject(new bat(),437,10);
            addObject(new bat(),400,10);        
            setBackground("Back5.bmp");
            closed=2;
        }
        if(l==6){
            map = new GreenfootImage("Map6.bmp");
            setBackground("Back6.bmp");
            addObject(new Zony(2,1),22,184);
            addObject(new elevator(136),75,202);
            addObject(new elevator(256),443,379);
            addObject(new lever(false),233,102);
            addObject(new lever(false),177,193);
            addObject(new lever(false),103,286);
            addObject(new door(34,0),483,357);
            addObject(new Enemy(true,true),389,52);
            addObject(new Enemy(true,true),364,52);
            addObject(new Enemy(true,true),314,52);
            addObject(new Enemy(true,true),263,52);
            addObject(new Enemy(true,true),210,52);
            closed=3;
        }
        if(l==7){
            map = new GreenfootImage("Map7.bmp");
            setBackground("Back7.bmp");
            addObject(new Zony(2,1),22,360);
            addObject(new Enemy(true,false),241,318);
            addObject(new Enemy(true,false),209,318);
            addObject(new Enemy(true,false),175,318);
            addObject(new elevator(120),330,379);
            addObject(new elevator(132),330,211);
            addObject(new elevator(300),70,379);
            addObject(new elevator(168),123,379);
            addObject( new lever(true),580,358);
            addObject( new lever(false),109,182);
            addObject( new lever(true),345,182);
            addObject( new lever(true),245,45);
            addObject( new door(74,0),585,37);
            addObject( new door(40,0),311,303);
            addObject( new door(40,0),281,303);
            addObject( new door(40,0),143,303);
            addObject(new bat(),434,87);
            addObject(new bat(),488,87);
            addObject(new bat(),440,237);
            addObject(new bat(),529,237);
            addObject(new bat(),565,237);
            closed=4;
        }
        if(l==8){
            map = new GreenfootImage("Map8.bmp");
            setBackground("Back8.bmp");
            addObject(new Zony(2,1),20,60);
            addObject(new Glob(),555,95);
            addObject(new Glob(),523,228);
            addObject(new Glob(),300,159);
            addObject(new Glob(),44,307);
            addObject(new lever(true),580,279);
            addObject(new lever(false),18,296);
            addObject(new door(100,0),585,49);
            closed=2;
        }
        if(l==9){
            map = new GreenfootImage("Map9.bmp");
            setBackground("Back9.bmp");
            addObject(new Zony(2,1),20,20);
            addObject(new platform(0,200,1,0,false),85,105);
            addObject(new platform(200,0,-1,0,false),526,105);
            addObject(new platform(0,200,0,1,false),564,92);
            addObject(new platform(0,200,1,0,false),304,290);
            addObject(new platform(100,0,-1,-1,false),264,290);
            addObject(new platform(0,100,1,0,false),27,300);
            addObject(new platform(0,300,3,0,false),60,375);
            addObject(new platform(100,0,-1,0,false),496,375);
            closed=1;
        }
        if(l==10){
            map = new GreenfootImage("Map10.bmp");
            setBackground("Back10.bmp");
            addObject(new Zony(2,1),20,86);
            addObject(new box(),150,130);
            addObject(new box(),248,66);
            addObject(new box(),466,90);
            addObject(new box(),518,66);
            addObject(new lever(false),26,292);
            addObject(new lever(true),426,363);
            addObject(new upgrade(3),517,349);
            addObject(new door(62,90),392,135);
            closed=2;
        }
        
        addObject(new overlay(),300,200);
    }
    
    
    public void done(int level){
        Zony z =  (Zony) getObjects(Zony.class).get(0);
        weapon=z.getWeapon();
        int l = getObjects(null).size();
        Actor a;
        for(int f=0;f<l;f++){
            a = (Actor) getObjects(null).get(0);
            removeObject(a);  
        }
        addObject(new control(level+1),300,200);
        Level(level+1);
    }
    
    public void open(){
        closed--;
    }
    
    public boolean openFull(){
        return closed==0;
    }
    
    public void stopped(){
        //sequencer.stop();
        setPaintOrder(control.class ,box.class,door.class,projectile.class,elevator.class,sword.class,upgrade.class,bat.class,fall.class,Tank.class,Enemy.class,bullet.class,platform.class,Zony.class,lever.class,overlay.class);
    }
    public void started(){
        //sequencer.start();
        setPaintOrder(control.class ,door.class,overlay.class,box.class,projectile.class,elevator.class,sword.class,upgrade.class,bat.class,fall.class,Tank.class,Enemy.class,bullet.class,platform.class,Zony.class,lever.class);
    }
    private void load(String fileName) {
        try {
            URL url = getClass().getClassLoader().getResource(fileName);
            if(url == null)
            throw new IOException("File not found: " + fileName);
            Sequence sequence = MidiSystem.getSequence(url);
            
            // Create a sequencer for the sequence
            sequencer = MidiSystem.getSequencer();
            sequencer.open();
            sequencer.setSequence(sequence);
        }
        catch (MalformedURLException e) {}
        catch (IOException e) {}
        catch (MidiUnavailableException e) {}
        catch (InvalidMidiDataException e) {}
    }
    
}